package com.nercel.dsj.gksales.service;

import com.nercel.dsj.gksales.shiro.util.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ExportService {

    public final static String BLANK_TPL = "blank.xlsx";

    public final static String ERROR_TPL = "error.xlsx";


    @Value("${app.upload.path}")
    protected String workspacePath;

    public String export(String tpl, List<List<String>> listData, Integer startIndex){
        return export(tpl, null, listData, startIndex);
    }

    public String export(String tpl, String specName, List<List<String>> listData, Integer startIndex){
        String filePath = workspacePath + "/export-templates/" + tpl;
        String filename = System.currentTimeMillis() + "_" + (specName == null ? tpl : (specName + ".xlsx"));
        String targetFilePath = workspacePath + "/temp/" + filename;
        try {
            File targetFile = new File(targetFilePath);
            FileUtil.copyFile(new File(filePath), targetFile);
            ExcelUtil.writeExcel(targetFile, listData, startIndex);
            return filename;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
